﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jumpton.Data;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;

namespace Jumpton.SFA.Server.BusinessRule.System
{
    public class RoleManager : BusinessLogic
    {
        /// <summary>
        /// 返回用户组的权限,用户分配多个组，合并组的权限
        /// </summary>
        /// <param name="GroupID"></param>
        /// <returns></returns>
        public DataTable GetUserRole(decimal UserID, int? type)
        {

            string sql = @"select  t2.MODULE_ITEM_ID,MODULES.NAME MODULE_NAME,MODULES.ICON MODULE_ICON,MODULE_ITEMS.NAME ITEM_NAME
		                            ,MODULE_ITEMS.LINK ITEM_LINK,MODULE_ITEMS.SMALL,MODULE_ITEMS.PAGE_INDEX,SOURCE,SOURCETYPE
                            from sys_User_roles t1
                                    inner join sys_role_resources t2 on t1.role_no=t2.GROUP_ID
		                            inner join MODULE_ITEMS on t2.MODULE_ITEM_ID=MODULE_ITEMS.ID
		                            inner join MODULES on MODULE_ITEMS.MODULE_ID=MODULES.ID
                            where MODULE_ITEMS.active='1' and MODULES.active='1'  and t1.ACTIVE='1' and
                             t1.user_id=" + UserID + (type == null ? "" : " and MODULE_ITEMS.type=" + type) +
                            @"
                             group by t2.MODULE_ITEM_ID, MODULES.NAME,MODULES.ICON,MODULE_ITEMS.NAME ,MODULE_ITEMS.LINK,MODULES.PAGE_INDEX,MODULE_ITEMS.PAGE_INDEX,MODULE_ITEMS.SMALL,SOURCE,SOURCETYPE
                            ORDER BY MODULES.PAGE_INDEX,MODULE_ITEMS.PAGE_INDEX";

            return Jumpton.Data.SqlData.OpenSql(sql).Tables[0];
        }

        

        public ObjectTable LoadModules()
        {
            return new ObjectTable(typeof(MODULES), "Active = '1'");
        }

        public ObjectTable LoadItems(decimal ModuleID)
        {
            return new ObjectTable(typeof(MODULE_ITEMS), "this.MODULEID='" + ModuleID + "' and Active = '1'");
        }

        public ObjectTable LoadAuth(decimal GroupID)
        {
            return new ObjectTable(typeof(SYS_ROLE_RESOURCES), "this.GROUP_ID='" + GroupID + "'");
        }

        public static bool IsExistRole(string GroupID, string ItemID)
        {
            SYS_ROLE_RESOURCES roles = (SYS_ROLE_RESOURCES)ObjectRow.Load(typeof(SYS_ROLE_RESOURCES), "this.GROUP_ID='" + GroupID + "' and this.RESNO='" + ItemID + "'");

            return (roles == null) ? false : true;
        }

        public static String GetUserRights(decimal UserID)
        {
            var dt = SqlData.OpenSql(@"select distinct upper(link) from dbo.SYS_USER_ROLES t1
                inner join dbo.SYS_ROLE_RESOURCES t2 on t1.role_no=t2.GROUP_ID
                inner join module_items t3 on t2.MODULE_ITEM_ID=t3.id 
                where t3.active=1 and t3.type=0 and t1.user_id=" + UserID).Tables[0];
            StringBuilder sb = new StringBuilder();

            foreach (DataRow row in dt.Rows)
            {
                sb.Append(row[0].ToString());
                sb.Append(",");
            }
            return sb.ToString();
        }

    }
}
